package com.test.daily.leetcode.y2022.m07.day0716.v02;

import java.util.Stack;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/16 上午 09:24
 * @version: 1.0
 */
public class Solution {
    public static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    public boolean isPar1(Node root) {
        Stack<Node> stack = new Stack<>();
        Node head = root;
        stack.push(head);
        while (head != null) {
            head = head.next;
            stack.push(head);
        }
        head = root;
        while (head != null) {
            if (stack.pop().value != head.value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

    public boolean isPar2(Node head) {
        Node faster = head;
        Node slower = head;
        while (faster.next != null && faster.next.next != null) {
            slower = slower.next;
            faster = faster.next.next;
        }
        Stack<Node> stack = new Stack<>();
        while (slower != null) {
            stack.push(slower);
        }
        Node root = head;
        while (!stack.isEmpty()) {
            if (root.value != stack.pop().value) {
                return false;
            }
            root = root.next;
        }
        return true;
    }

    public boolean isPar3(Node head) {
        Node n1 = head;
        Node n2 = head;
        while (n2.next != null || n2.next.next != null) {
            n1 = n1.next;
            n2 = n2.next.next;
        }

        n2 = n1.next;
        n1.next = null;

        Node n3 = null;
        while (n2 != null) {
            n3 = n2.next;
            n2.next = n1;
            n1 = n2;
            n2 = n3;
        }
        n2 = head;
        n1 = n1;
        boolean ans = true;
        while(n1!=null && n2 != null){
            if(n1.value != n2.value){
                ans = false;
            }
            n1= n1.next;
            n2 = n2.next;
        }
        // 反转表格, n3存储着最后一个节点
        n1 = n3;
        n2 = null;
        while(n1 != null){
            n3 = n1.next;
            n1.next = n2;
            n2 = n1;
            n1 = n3;
        }
        return ans;
    }
}
